package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.SaltNotes;

public class SaltNotesPresenter extends AbstractPresenter<SaltNotes> {
	final String SINGLEITEM = "saltNotes";
	final String MULTIITEM = "saltNotess";
	
	public ArrayList<SaltNotes> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public SaltNotes tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public SaltNotes createItemFromJSONObject(JSONObject jsonObject){
		SaltNotes item = new SaltNotes();
		try{
			item.setId(jsonObject.getInt("saltNoteID"));
			item.setNotes(jsonObject.getString("saltNotes"));
			item.setGrams(jsonObject.getDouble("saltGrams"));
			
			WaterSaltsPresenter presenter = new WaterSaltsPresenter();
			JSONObject jsonGroup = jsonObject.getJSONObject("waterSaltID");
			item.setSalt(presenter.createItemFromJSONObject(jsonGroup));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(SaltNotes item){
		try{
			JSONObject json = new JSONObject();
			json.put("saltNoteID", item.getId());
			json.put("saltNotes", item.getNotes());
			json.put("saltGrams", item.getGrams());

			WaterSaltsPresenter presenter = new WaterSaltsPresenter();
			json.put("waterSaltID", presenter.createJSON(item.getSalt()));
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public SaltNotes getEmptyEntity(){
		SaltNotes entity = new SaltNotes();
		return entity;
	}
}
